package com.example.shop.shops.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.shop.base.config.BussinessException;
import com.example.shop.base.constant.RegisterCon;
import com.example.shop.ov.UserDto;
import com.example.shop.shops.domain.Logininfo;
import com.example.shop.shops.domain.User;
import com.example.shop.shops.mapper.LogininfoMapper;
import com.example.shop.shops.mapper.UserMapper;
import com.example.shop.shops.service.UserService;
import com.example.shop.util.MD5Utils;
import com.example.shop.util.RegexUtils;
import com.example.shop.util.StrUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author Hasee
 * @description 针对表【t_user】的数据库操作Service实现
 * @createDate 2022-11-24 14:15:50
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void gteLogin(UserDto userDto) {
        // 判断密码是否一样
        if (!userDto.getPasswordRepeat().equals(userDto.getPassword())) {
            throw new BussinessException("两次密码不一致！");
        }
        // 验证码是否正确
        String smsCode = (String) redisTemplate.opsForValue().get(String.format(RegisterCon.REGISTER_PHONE_KEY, userDto.getPhone()));
        if (StringUtils.isBlank(smsCode) || !smsCode.equals(userDto.getSmsCode())) {
            throw new BussinessException("验证码错误！");
        }

        if (!RegexUtils.isChinaPhoneLegal(userDto.getPhone())) {
            throw new BussinessException("手机号码非法");
        }
        User user = baseMapper.selectOne(new QueryWrapper<User>().eq("phone", userDto.getPhone()));
        if (user != null) {
            throw new BussinessException("你已经注册过了");
        }
        this.getAdd(userDto);

    }

    @Override
    public void getEmaidAdd(UserDto userDto) {
        // 判断密码是否一样
        if (!userDto.getPasswordRepeat().equals(userDto.getPassword())) {
            throw new BussinessException("两次密码不一致！");
        }
        // 验证码是否正确
        String smsCode = (String) redisTemplate.opsForValue().get("Email" + userDto.getEmail());
        System.out.println("---------------------------------");
        System.out.println(smsCode);
        System.out.println("-----------------------------------");
        if (StringUtils.isBlank(smsCode) || !smsCode.equals(userDto.getSmsCode())) {
            throw new BussinessException("验证码错误！");
        }
        this.getInsert(userDto);
    }

    /**
     * 邮箱注册大法
     *
     * @param userDto
     */
    private void getInsert(UserDto userDto) {
        // 添加数据，连表更新
        User closer = UserDto.getUser();
        closer.setUsername(userDto.getEmail());
        closer.setEmail(userDto.getEmail());
        // 密码加密 ，颜值
        String password = userDto.getPassword(); // 获取到密码
        String salt = StrUtils.getComplexRandomString(32);
        String s = MD5Utils.encrypByMd5(salt);

        closer.setSalt(s);
        closer.setPassword(MD5Utils.encrypByMd5(password) + s);

        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(closer, logininfo);
        logininfo.setEmail(userDto.getEmail());
        logininfo.setDisable(true);
        logininfo.setType(1);
        logininfoMapper.insert(logininfo);
        closer.setLogininfoId(logininfo.getId());
        baseMapper.insert(closer);
    }

    /**
     * 电话注册大法
     *
     * @param userDto
     */
    private void getAdd(UserDto userDto) {
        // 添加数据，连表更新
        User closer = UserDto.getUser();
        closer.setUsername(userDto.getPhone());
        closer.setPhone(userDto.getPhone());
        // 密码加密 ，颜值
        String password = userDto.getPassword(); // 获取到密码
        String salt = StrUtils.getComplexRandomString(32);
        String s = MD5Utils.encrypByMd5(salt);
        closer.setSalt(s);
        closer.setPassword(MD5Utils.encrypByMd5(password) + s);
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(closer, logininfo);
        logininfo.setPhone(userDto.getPhone());
        logininfo.setDisable(true);
        logininfo.setType(1);
        logininfoMapper.insert(logininfo);
        closer.setLogininfoId(logininfo.getId());
        baseMapper.insert(closer);
    }


}





